<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
      <span @click="deleteAllSearchHistories">全部删除</span>&nbsp;
      <span @click="isDeleteShow=false">完成</span>
      </div>
      <van-icon name="delete" v-else @click="isDeleteShow=true"></van-icon>
    </van-cell>
    <!-- 历史记录 -->
    <van-cell :title="item" v-for="(item,index) in searchHistories" :key="item.id" @click="onSearchItemClick(item,index)"><van-icon name="close" v-if="isDeleteShow"></van-icon></van-cell>
  </div>
</template>

<script>
import { ref } from 'vue'
export default {
  name: 'SearchHistory',
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },
  setup (props, context) {
    const isDeleteShow = ref(false)// 是否处于编辑状态
    // 全部删除
    function deleteAllSearchHistories () {
      context.emit('deleteAllSearchHistories')
    }
    function onSearchItemClick (item, index) {
      if (isDeleteShow.value) {
        context.emit('deleteSearchHistory', index)// 删除
      } else {
        context.emit('search', item)// 搜索
      }
    }
    return {
      isDeleteShow,
      onSearchItemClick,
      deleteAllSearchHistories
    }
  }
}
</script>

<style>

</style>
